package com.example.vertification.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.vertification.Utils.RedisUtil;
import com.example.vertification.entity.*;
import com.example.vertification.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
@Api(value = "问卷系统", tags = "问卷系统")
public class QuestionController {

    private final UserService userService;
    private final QuestionService questionService;
    private  final UserStaticsService userStaticsService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    public QuestionController(UserService userService, QuestionService questionService,UserStaticsService userStaticsService) {
        this.userService = userService;
        this.questionService = questionService;
        this.userStaticsService = userStaticsService;
    }



    @GetMapping("/get_random_questions")
    @ApiOperation(value = "传入数据为用户的token，获取随机题库")
    public ResponseEntity<?> set_shop_desc(@RequestParam String token) throws IOException {
        User u = userService.getUserByToken(token);
        if(u==null){
            return ResponseEntity.ok("请先登入!");
        }
        if(!redisUtil.canAnswerQuestion(u.getUid().toString())){
            return ResponseEntity.ok("请明日再来答题!");
        }
        List<Question> allQuestions = questionService.getAllQuestions();
        if (allQuestions.size() < 5) {
            // 如果问题总数少于5个，则直接返回所有问题
            return ResponseEntity.ok(allQuestions);
        }
        // 创建一个新的列表来存储随机选取的问题，避免修改原始列表
        List<Question> randomQuestions = new ArrayList<>(allQuestions);

        // 随机排序列表
        Collections.shuffle(randomQuestions);
        // 取前5个问题
        List<Question> fiveRandomQuestions = randomQuestions.subList(0, 5);
        return ResponseEntity.ok(fiveRandomQuestions);
    }

    @PostMapping("/get_submit_questions")
    @ApiOperation(value = "传入数据为token,以及用户的答题结果results")
    public ResponseEntity<?> get_submit_questions(@RequestBody String body) throws IOException {

        JSONObject obj = (JSONObject) JSONUtil.parse(body);
        String token = obj.getStr("token");
        String result = obj.getStr("results");

        JSONArray list = JSONUtil.parseArray(result);
        User u = userService.getUserByToken(token);
        if(u==null) {
            return ResponseEntity.ok("请先登入!");
        }
        int yes =0;
        for(int i =0;i<list.size();i++){
            if(list.get(i).toString().equalsIgnoreCase("Y")){
                yes++;
            }
        }

        UserStatics user_infos = userStaticsService.getEntityByUid(u.getUid());
        if(user_infos==null){
            user_infos = new UserStatics(u.getUid(),0,0);
        }
        user_infos.setQuestionYes(user_infos.getQuestionYes()+yes);
        user_infos.setQuestionNo(user_infos.getQuestionNo()+(5-yes));
        userStaticsService.save(user_infos);
        u.setScore(u.getScore()+5*yes);
        userService.saveUser(u);
        return ResponseEntity.ok("提交成功,获得了"+5*yes+"积分!");
    }



}
